package list

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/28 16:43
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type Node struct {
	Key      interface{}
	Value    interface{}

	Pre  *Node
	Next *Node

	Left  *Node
	Right *Node

	Parent *Node
	Down   *Node
}

func (m *Node) Node2Array(root *Node) []int {
	if root == nil {
		return []int{}
	}
	array := make([]int, 0)
	for root != nil {
		array = append(array, root.Value.(int))
		root = root.Next
	}
	return array
}

func (m *Node) Array2Node(array []int) *Node {
	if len(array) == 0 {
		return nil
	}
	root := &Node{Value: array[0]}
	head := root
	for i := 1; i < len(array); i++ {
		root.Next = &Node{
			Value: array[i],
		}
		root = root.Next
	}
	return head
}

func (m *Node) Equals(a, b *Node) bool {
	if a == nil && b == nil {
		return true
	}
	if a == nil || b == nil {
		return false
	}
	for a != nil && b != nil {
		if a.Value != b.Value {
			return false
		}
		a = a.Next
		b = b.Next
	}
	return a == nil && b == nil
}

func (m *Node) TreeEquals(a, b *Node) bool {
	if a == nil && b == nil {
		return true
	}
	if a == nil || b == nil {
		return false
	}
	if a.Value != b.Value {
		return false
	}
	return m.TreeEquals(a.Left, b.Left) && m.Equals(a.Right, b.Right)
}
